package com.example.cd5160866.modelrecycleview.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cd5160866.modelrecycleview.R;
import com.example.cd5160866.modelrecycleview.adapter.ModelRecyclerAdapterV2;
import com.example.cd5160866.modelrecycleview.adapter.RecyclerItemViewId;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by cd5160866 on 2016/10/8.
 */
public class Demo3Activity extends DemoBaseActivity {
    @BindView(R.id.rv_demo_base)
    RecyclerView recyclerView;
    private static final int DEFAULT_GIRD_SPAN = 3;
    private ModelRecyclerAdapterV2<String> adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);

        adapter = new ModelRecyclerAdapterV2<>(this, MyImageViewHolder.class, data);
        recyclerView.setAdapter(adapter);
    }

    @RecyclerItemViewId(R.layout.item_list)
    public static class MyImageViewHolder extends ModelRecyclerAdapterV2.ModelViewHolder<String> {
        @BindView(R.id.iv_item1)
        ImageView imageView;

        @OnClick(R.id.iv_item1)
        void onclick() {
            Toast.makeText(imageView.getContext(), position + " 点击~", Toast.LENGTH_SHORT).show();
        }

        private int position;

        public MyImageViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void update(String item, ModelRecyclerAdapterV2 adapter, int position) {
            this.position = position;
            Picasso.with(adapter.getContext()).load(item).into(imageView);
        }

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        LinearLayoutManager manager;
        switch (item.getItemId()) {
            case R.id.list_vertical:
                manager = new LinearLayoutManager(this);
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(manager);
                break;
            case R.id.list_horizontal:
                manager = new LinearLayoutManager(this);
                manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                recyclerView.setLayoutManager(manager);
                break;
            case R.id.gridview:
//                manager = new GridLayoutManager(this, DEFAULT_GIRD_SPAN);
                                manager = getGridLayoutManager();
                recyclerView.setLayoutManager(manager);
                break;
            case R.id.add:
                adapter.add("http://a1.att.hudong.com/28/57/20300534506289144593571229462_s.jpg");
                break;
            case R.id.delete:
                if (adapter.getItemCount() >= 1) {
                    adapter.remove(adapter.getItemCount() - 1);
                }
                break;
            case R.id.add_header:
                adapter.setHeaderView(getTestHeader());
                break;
            case R.id.add_footer:
                adapter.setFooterView(getFooterView());
                break;
            case R.id.remove_header:
                adapter.setHeaderView(null);
                break;
            case R.id.remove_footer:
                adapter.setFooterView(null);
                break;
            default:
                break;
        }
        return true;
    }

    GridLayoutManager getGridLayoutManager() {
        final GridLayoutManager manager = new GridLayoutManager(this, DEFAULT_GIRD_SPAN);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int returanValue;
                if (adapter == null) {
                    returanValue = -1;
                } else {

                    if (adapter.getItemViewType(position) == ModelRecyclerAdapterV2.TYPE_DEFAULT) {
                        returanValue = 1;
                    } else {
                        returanValue = DEFAULT_GIRD_SPAN;
                    }
                }
                Log.d("qw", "position " + position + "  span" + returanValue);
                return returanValue;
            }
        });
        return manager;
    }

    private View getTestHeader() {
        TextView view = (TextView) LayoutInflater.from(this).inflate(R.layout.item_footer, recyclerView, false);
        view.setText("this is Header");
        view.setBackgroundColor(Color.BLUE);
        return view;
    }

    private View getFooterView() {
        TextView footerText = (TextView) LayoutInflater.from(this).inflate(R.layout.item_footer, recyclerView, false);
        footerText.setText("this is footer");
        footerText.setBackgroundColor(Color.RED);
        return footerText;
    }

    private View getHeaderPager() {
        ViewPager vpHeader = (ViewPager) LayoutInflater.from(this).inflate(R.layout.item_header, recyclerView, false);
        vpHeader.setAdapter(new GalleryAdapter());
        return vpHeader;
    }

    public class GalleryAdapter extends PagerAdapter {

        private int getScreenWidth(Context context) {
            DisplayMetrics metric = new DisplayMetrics();
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            manager.getDefaultDisplay().getMetrics(metric);
            return metric.widthPixels;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object instantiateItem(final ViewGroup container, final int position) {
            ImageView imageView = new ImageView(container.getContext());
            int color = 0;
            switch (position) {
                case 0:
                    color = Color.RED;
                    break;
                case 1:
                    color = Color.BLACK;
                    break;
                case 2:
                    color = Color.WHITE;
                    break;
                case 3:
                    color = Color.GRAY;
                    break;
                case 4:
                    color = Color.YELLOW;
                    break;
                default:
                    break;
            }
            imageView.setBackgroundColor(color);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(container.getContext(), position + " header", Toast.LENGTH_SHORT).show();
                }
            });
            Picasso.with(container.getContext()).load(data.get(position)).into(imageView);
            container.addView(imageView);
            ViewGroup.LayoutParams params = imageView.getLayoutParams();
            params.width = getScreenWidth(imageView.getContext());
            params.height = params.width / 3;
            imageView.setLayoutParams(params);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }


}
